package net.topicopedia.servlets.categories;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;

@SuppressWarnings("serial")
public class ResetCategoriesServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
		      throws IOException {
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		
		resp.addHeader("Access-Control-Allow-Origin", "*");
		resp.setContentType("text/plain");
		
		Query categoryQuery = new Query("Category");
		
		PreparedQuery pq = datastore.prepare(categoryQuery);
		
		for (Entity category : pq.asIterable() ) {
			category.setProperty("count", 0);
			Key categoryKey = datastore.put( category );
			
			resp.getWriter().println(String.valueOf( category.getProperty("name") ));
			resp.getWriter().println(" category id :: " + categoryKey.getId() );
		}
		
	}

}
 